package com.ruoyi.limitCoupon.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.limitCoupon.domain.LimitCouponConfig;
import com.ruoyi.limitCoupon.domain.LimitCouponMatchesConfig;
import com.ruoyi.limitCoupon.mapper.LimitCouponConfigMapper;
import com.ruoyi.limitCoupon.mapper.LimitCouponMatchesConfigMapper;
import com.ruoyi.limitCoupon.service.ILimitCouponMatchesConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 限时卡券场次Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class LimitCouponMatchesConfigServiceImpl implements ILimitCouponMatchesConfigService {
    @Autowired
    private LimitCouponMatchesConfigMapper limitCouponMatchesConfigMapper;
    @Resource
    private LimitCouponConfigMapper limitCouponConfigMapper;

    /**
     * 查询限时卡券场次
     *
     * @param id 限时卡券场次主键
     * @return 限时卡券场次
     */
    @Override
    public LimitCouponMatchesConfig selectLimitCouponMatchesConfigById(Long id) {
        LimitCouponMatchesConfig limitCouponMatchesConfig = limitCouponMatchesConfigMapper.selectLimitCouponMatchesConfigById(id);
        limitCouponMatchesConfig.setSnappedTimeVar(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(limitCouponMatchesConfig.getSnappedTime()));
        return limitCouponMatchesConfig;
    }

    /**
     * 查询限时卡券场次列表
     *
     * @param limitCouponMatchesConfig 限时卡券场次
     * @return 限时卡券场次
     */
    @Override
    public List<LimitCouponMatchesConfig> selectLimitCouponMatchesConfigList(LimitCouponMatchesConfig limitCouponMatchesConfig) {
        return limitCouponMatchesConfigMapper.selectLimitCouponMatchesConfigList(limitCouponMatchesConfig);
    }

    /**
     * 新增限时卡券场次
     *
     * @param limitCouponMatchesConfig 限时卡券场次
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int insertLimitCouponMatchesConfig(LimitCouponMatchesConfig limitCouponMatchesConfig) throws ParseException {
        limitCouponMatchesConfig.setCreateTime(DateUtils.getNowDate());
        limitCouponMatchesConfig.setSnappedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(limitCouponMatchesConfig.getSnappedTimeVar()));
        int inserted = limitCouponMatchesConfigMapper.insertLimitCouponMatchesConfig(limitCouponMatchesConfig);
        if (!CollectionUtils.isEmpty(limitCouponMatchesConfig.getLimitCouponConfigs())) {
            for (LimitCouponConfig limitCouponConfig : limitCouponMatchesConfig.getLimitCouponConfigs()) {
                LimitCouponConfig insertLimitCouponConfig = new LimitCouponConfig();
                insertLimitCouponConfig.setMatchesId(limitCouponMatchesConfig.getId());
                insertLimitCouponConfig.setCouponFull(limitCouponConfig.getCouponFull());
                insertLimitCouponConfig.setCouponSubtract(limitCouponConfig.getCouponSubtract());
                insertLimitCouponConfig.setCouponStatus(limitCouponConfig.getCouponStatus());
                insertLimitCouponConfig.setCouponTotal(limitCouponConfig.getCouponTotal());
                insertLimitCouponConfig.setCouponLimitReceiveTotal(limitCouponConfig.getCouponLimitReceiveTotal());
                insertLimitCouponConfig.setCouponScopeApplication(limitCouponConfig.getCouponScopeApplication());
                insertLimitCouponConfig.setEffectDay(limitCouponConfig.getEffectDay());
                limitCouponConfigMapper.insertLimitCouponConfig(insertLimitCouponConfig);
            }
        }
        return inserted;
    }

    /**
     * 修改限时卡券场次
     *
     * @param limitCouponMatchesConfig 限时卡券场次
     * @return 结果
     */
    @Override
    public int updateLimitCouponMatchesConfig(LimitCouponMatchesConfig limitCouponMatchesConfig) throws ParseException {
        if (!CheckEmptyUtil.isEmpty(limitCouponMatchesConfig.getSnappedTimeVar())) {
            limitCouponMatchesConfig.setSnappedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(limitCouponMatchesConfig.getSnappedTimeVar()));
        }
        limitCouponMatchesConfig.setUpdateTime(DateUtils.getNowDate());
        return limitCouponMatchesConfigMapper.updateLimitCouponMatchesConfig(limitCouponMatchesConfig);
    }

    /**
     * 批量删除限时卡券场次
     *
     * @param ids 需要删除的限时卡券场次主键
     * @return 结果
     */
    @Override
    public int deleteLimitCouponMatchesConfigByIds(Long[] ids) {
        return limitCouponMatchesConfigMapper.deleteLimitCouponMatchesConfigByIds(ids);
    }

    /**
     * 删除限时卡券场次信息
     *
     * @param id 限时卡券场次主键
     * @return 结果
     */
    @Override
    public int deleteLimitCouponMatchesConfigById(Long id) {
        return limitCouponMatchesConfigMapper.deleteLimitCouponMatchesConfigById(id);
    }
}
